package com.telecom.cms.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.telecom.cms.util.Constant;
import com.telecom.cms.dto.*;
import com.telecom.cms.util.CurrentTime;

public class IDA_TEMPLATE_ATOM_RESULTDAO {
	private static final String SQL_FINDBYID = "select * from "
		+ Constant.IDA_TEMPLATE_ATOM_RESULT + " where Template_Id = ? order by Display_Id";

	private static final String SQL_INSERT = "insert into "
			+ Constant.IDA_TEMPLATE_ATOM_RESULT
			+ " (Template_Id,Biz_Atom_Id,Biz_Atom_Name,Calc_Rule,Result_Order_Rule,Display_Id)Values(?,?,?,?,?,?)";

	private static final String SQL_DELETE = "delete from "
			+ Constant.IDA_TEMPLATE_ATOM_RESULT + " where Template_Id = ?";

	private Connection conn = null;

	public IDA_TEMPLATE_ATOM_RESULTDAO(Connection con) {

		this.conn = con;

	}

	public List findById(int TemplateId) {

		PreparedStatement psmt = null;
		ResultSet rs = null;
		List list = new ArrayList();
		try {
			psmt = conn.prepareStatement(SQL_FINDBYID);
			psmt.setInt(1, TemplateId);
			rs = psmt.executeQuery();
			while (rs.next()) {
				IDA_TEMPLATE_ATOM_RESULTDTO ida_template_atom_resultdto = new IDA_TEMPLATE_ATOM_RESULTDTO();
				ida_template_atom_resultdto.setBizAtomId((rs
						.getInt("Biz_Atom_Id")));
				ida_template_atom_resultdto.setTemplateId(rs.getInt("Template_Id"));
				ida_template_atom_resultdto.setBizAtomName(rs
						.getString("Biz_Atom_Name"));
				ida_template_atom_resultdto.setCalcRule(rs.getString("Calc_Rule"));
				ida_template_atom_resultdto.setResultOrderRule(rs.getString("Result_Order_Rule"));
				ida_template_atom_resultdto.setDisplayId(rs.getInt("Display_Id"));
				list.add(ida_template_atom_resultdto);
				}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (rs != null) {

					rs.close();
				}
				if (psmt != null) {
					psmt.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return list;
	}

	public void insert(IDA_TEMPLATE_ATOM_RESULTDTO ida_template_atom_result) {

		PreparedStatement psmt = null;
		try {
			psmt = conn.prepareStatement(SQL_INSERT);
			psmt.setInt(1, ida_template_atom_result.getTemplateId());
			psmt.setInt(2, ida_template_atom_result.getBizAtomId());
			psmt.setString(3, ida_template_atom_result.getBizAtomName());
			psmt.setString(4, ida_template_atom_result.getCalcRule());
			psmt.setString(5, ida_template_atom_result.getResultOrderRule());
			psmt.setInt(6, ida_template_atom_result.getDisplayId());
			psmt.execute();

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (psmt != null) {
					psmt.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public void delete(int templateId) {

		PreparedStatement psmt = null;
		try {
			psmt = conn.prepareStatement(SQL_DELETE);
			psmt.setInt(1, templateId);
			psmt.execute();

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {

				if (psmt != null) {
					psmt.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}